*****************************************************************************
* EGRESSIONS --- ASER SCORES ARE TREATED AS INDIVIDUAL COMPETENCIES
*****************************************************************************

preserve 

foreach var of varlist *read *math {
	gen `var'2 = `var'- 3
	replace `var'2 = 0 if `var'2<0
}

qui tab read2, gen(readlevel)
qui tab math2, gen(mathlevel)

la var el_read2 "ASER Reading"
la var el_math2 "ASER Math"

la var readlevel1 "Word"
la var readlevel2 "Paragraph"
la var readlevel3 "Story"

la var mathlevel1 "Digits"
la var mathlevel2 "Subtraction"
la var mathlevel3 "Division"

* Set 1: OLS REGRESSIONS -- Constrains the sample to a baseline level and looks at whether the endline score differs across control and treatment for that level.

est clear
forval i = 1/3 {
	local depvar = "el_read2"
	local baselevel = "readlevel`i'"
	*Running the statistical inference
	qui ritest treatment _b[treatment], cluster(id_school) reps($reps): ///
	reg `depvar' treatment if `baselevel' == 1, cluster(id_school)
	matrix pvalues = r(p)
	gen temp = pvalues[1,1]

	eststo panela_`depvar'`i': qui bootstrap, seed(2905) cl(id_school) reps($reps ) nodots: qui reg `depvar' treatment if `baselevel' == 1
*	eststo: reg `var'_el treatment base miss, vce(robust)
	qui test treatment 
	qui estadd scalar pvalreg = round(r(p),0.001)
	qui summ `depvar' if treatment == 0 & `baselevel' == 1
	qui estadd scalar mean = r(mean)
	qui estadd local ife = "No", replace
	qui estadd local sfe = "No", replace
	qui estadd scalar rsq = e(r2)
	qui summ temp
	qui estadd scalar pvalri= r(mean)
	drop temp
}

forval i = 1/3 {
	local depvar = "el_math2"
	local baselevel = "mathlevel`i'"

	*Running the statistical inference
	qui ritest treatment _b[treatment], cluster(id_school) reps($reps): ///
	reg `depvar' treatment if `baselevel' == 1, cluster(id_school)
	matrix pvalues = r(p)
	gen temp = pvalues[1,1]

	eststo panela_`depvar'`i': qui bootstrap, seed(2905) cl(id_school) reps($reps ) nodots: qui reg `depvar' treatment if `baselevel' == 1
*	eststo: reg `var'_el treatment base miss, vce(robust)
	qui test treatment 
	qui estadd scalar pvalreg = round(r(p),0.001)
	qui summ `depvar' if treatment == 0 & `baselevel' == 1
	qui estadd scalar mean = r(mean)
	qui estadd local ife = "No", replace
	qui estadd local sfe = "No", replace
	qui estadd scalar rsq = e(r2)
	qui summ temp
	qui estadd scalar pvalri= r(mean)
	drop temp

}


* b. With individual and school controls
forval i = 1/3 {
	local depvar = "el_read2"
	local baselevel = "readlevel`i'"

	*Running the statistical inference
	qui ritest treatment _b[treatment], cluster(id_school) reps($reps): ///
	reg `depvar' treatment $indcontrols $schcontrols if `baselevel' == 1, cluster(id_school)
	matrix pvalues = r(p)
	gen temp = pvalues[1,1]

	eststo panelb_`depvar'`i': qui bootstrap, seed(2905) cl(id_school) reps($reps ) nodots: qui reg `depvar' treatment $indcontrols $schcontrols if `baselevel' == 1
*	eststo: reg `var'_el treatment base miss, vce(robust)
	qui test treatment 
	qui estadd scalar pvalreg = round(r(p),0.001)
	qui summ `depvar' if treatment == 0 & `baselevel' == 1
	qui estadd scalar mean = r(mean)
	qui estadd local ife = "Yes", replace
	qui estadd local sfe = "Yes", replace
	qui estadd scalar rsq = e(r2)
	qui summ temp
	qui estadd scalar pvalri= r(mean)
	drop temp
}

forval i = 1/3 {
	local depvar = "el_math2"
	local baselevel = "mathlevel`i'"

	*Running the statistical inference
	qui ritest treatment _b[treatment], cluster(id_school) reps($reps): ///
	reg `depvar' treatment $indcontrols $schcontrols if `baselevel' == 1, cluster(id_school)
	matrix pvalues = r(p)
	gen temp = pvalues[1,1]

	eststo panelb_`depvar'`i': qui bootstrap, seed(2905) cl(id_school) reps($reps ) nodots: qui reg `depvar' treatment $indcontrols $schcontrols if `baselevel' == 1
*	eststo: reg `var'_el treatment base miss, vce(robust)
	qui test treatment 
	qui estadd scalar pvalreg = round(r(p),0.001)
	qui summ `depvar' if treatment == 0 & `baselevel' == 1
	qui estadd scalar mean = r(mean)
	qui estadd local ife = "Yes", replace
	qui estadd local sfe = "Yes", replace
	qui estadd scalar rsq = e(r2)
	qui summ temp
	qui estadd scalar pvalri= r(mean)
	drop temp

}	

# delimit ;
esttab  panela* using "$tables/TableA1.tex", replace
keep(treatment)
cells(b(fmt(%5.2f) star) se(fmt(%5.2f) par))
starlevels(* .10 ** .05 *** .01) 
mgroups("Reading Level" "Math Level", pattern(1 0 0 1 0 0)
span prefix(\multicolumn{@span}{c}{) suffix(}) erepeat(\cmidrule(lr){@span})) 
mtitle("Word" "Paragraph" "Story" "Digits" "Subtraction" "Division")
posthead("\midrule \multicolumn{7}{l}{\textit{\textbf{Panel A. Without individual and school controls}}} \\ \midrule")
postfoot("")
stats(pvalreg pvalri rsq, labels("p-val (OLS)" "p-val (RI)" "\midrule R2") ///
fmt(2 2 2)) ///
collabels(none) label booktabs nonotes
;

# delimit ;
esttab  panelb* using "$tables/TableA1.tex", append
keep(treatment) ///
cells(b(fmt(%5.2f) star) se(fmt(%5.2f) par)) /// 
starlevels(* .10 ** .05 *** .01) ///
prehead("") ///
posthead("\midrule \multicolumn{7}{l}{\textit{\textbf{Panel B. With individual and school controls}}} \\ \midrule")
stats(pvalreg pvalri rsq N, labels("p-val (OLS)" "p-val (RI)" "\midrule R2" "N") ///
fmt(2 2 2 0)) ///
collabels(none) mlabels(none) nonumbers label booktabs nonotes nomtitles;
#delimit cr	


restore 